package com.own.business.system.dictionary.controller;

import com.own.aop.permission.PermissionModule;
import com.own.aop.permission.PermissionOperation;
import com.own.business.system.dictionary.business.service.SystemDictionaryService;
import com.own.business.system.dictionary.common.constant.ConstantModule;
import com.own.business.system.dictionary.entity.dto.SystemDictionaryDto;
import com.own.business.system.dictionary.entity.query.SystemDictionaryQuery;
import com.own.business.system.dictionary.entity.vo.SystemDictionaryVo;
import com.own.component.common.base.controller.BaseController;
import com.own.component.common.model.PageModel;
import com.own.component.common.model.R;
import com.own.component.common.model.ResultModel;
import com.own.component.store.core.confirm.aop.ConfirmAop;
import com.own.constant.ConstantApiPrefix;
import com.own.constant.ConstantBeanSuffix;
import com.own.constant.ConstantPermission;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * SystemDictionaryAdapterManageParentController
 *
 * @author chenxueli
 * @date 2023-06-21 09:53:26
 */
@Tag(name = "数据字典类型" + ConstantBeanSuffix.CONTROLLER)
@PermissionModule(ConstantModule.SYSTEM_DICTIONARY)
@RestController
@RequestMapping(ConstantApiPrefix.BASE_API_V1_PREFIX + "system/dictionary")
public class SystemDictionaryController extends BaseController {

    @Resource
    private SystemDictionaryService systemDictionaryService;

    @Operation(summary = "查询全部信息")
    @PostMapping("/all")
    public ResultModel<List<SystemDictionaryVo>> all(
            @RequestBody SystemDictionaryQuery query
    ) {
        var pageModel = systemDictionaryService.all(query);
        return R.success(pageModel);
    }

    @PermissionOperation(ConstantPermission.SEARCH)
    @Operation(summary = "分页查询信息")
    @PostMapping("/page")
    public ResultModel<PageModel<SystemDictionaryVo>> page(
            @RequestBody SystemDictionaryQuery query
    ) {
        var pageModel = systemDictionaryService.page(query);
        return R.success(pageModel);
    }

    @PermissionOperation(ConstantPermission.ADD)
    @Operation(summary = "添加信息")
    @PostMapping("/")
    public ResultModel<SystemDictionaryVo> add(
            @Validated @RequestBody SystemDictionaryDto dto
    ) {
        dto.check();
        var updateModel = systemDictionaryService.add(dto.toBo());
        return R.success(updateModel);
    }

    @PermissionOperation(ConstantPermission.UPDATE)
    @Operation(summary = "修改信息")
    @PutMapping("/{systemDictionaryId}")
    public ResultModel<SystemDictionaryVo> update(
            @Parameter(description = "主键id", required = true) @PathVariable Long systemDictionaryId,
            @Validated @RequestBody SystemDictionaryDto dto
    ) {
        dto.check();
        var updateModel = systemDictionaryService.update(systemDictionaryId, dto.toBo());
        return R.success(updateModel);
    }

    @PermissionOperation(ConstantPermission.DELETE)
    @ConfirmAop(message = "确认删除该数据字典类型？（注意：删除之后无法找回数据，且会删除所有的该类型的项目数据）")
    @Operation(summary = "删除信息")
    @DeleteMapping("/{systemDictionaryId}")
    public ResultModel<Boolean> delete(
            @Parameter(description = "主键id", required = true) @PathVariable Long systemDictionaryId
    ) {
        var updateModel = systemDictionaryService.delete(systemDictionaryId);
        return R.success(updateModel);
    }

    @PermissionOperation(ConstantPermission.VIEW)
    @Operation(summary = "根据id获取信息")
    @GetMapping("/{systemDictionaryId}")
    public ResultModel<SystemDictionaryVo> getById(
            @Parameter(description = "主键id", required = true) @PathVariable Long systemDictionaryId
    ) {
        var vo = systemDictionaryService.getById(systemDictionaryId);
        return R.success(vo);
    }

}
